<?php
  class SiteParser
  {
    public static function parseFundeal($url, $debug = false)
    {
      $offers = array();
      //$offers[] = SiteParser::parseFundealOffer($url, $debug);
      
      $extra_offers = SiteParser::parseFundealForExtraOffers($url);
      foreach($extra_offers as $extra_offer)
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseFundealForExtraOffers($extra_offer));
      }
      
      $extra_offers = array_unique($extra_offers);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseFundealOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseFundealForExtraOffers($url)
    {
      $extra_offers = array();
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if(($div->getAttribute('class') == 'deal-title' || $div->getAttribute('class') == 'deal-info') && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'h2' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'a')
                {
                  $extra_offers[] = $childNode2->getAttribute('href');
                  break;
                }
              }
            }
          }
        }
      }

      return $extra_offers;
    }

    public static function parseFundealOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $price = null;
      $full_price = null;
      $offer_id = null;
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = Utils::curl_exec_follow($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      /*
      $h1s = $res->getElementsByTagName('h1');
      
      for($i = 0; $i < $h1s->length; $i++)
      {
        $h1 = $h1s->item($i);
        $attributes = $h1->attributes;
        if(!is_null($attributes))
        {
            foreach ($attributes as $index=>$attr)
            {
                if($attr->name == "class" && $attr->value == "dealhead")
                {
                  $offer_title = $h1->nodeValue;
                  break;
                }
            }
        }
      }
      */
      
      $offer_text = Utils::subString($html,'<div class="deal-title">','</div>');
      $offer_title = trim(strip_tags(Utils::subString($offer_text,'<h1>','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($offer_text,'<h2>','</h2>')));
      
      $div_with_form = $res->getElementById('buy-now');
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'buy-now' && $div->hasChildNodes())
        {
          $childNodes = $div->childNodes;
          for($i = 0; $i < $childNodes->length; $i++)
          {
            $childNode = $childNodes->item($i);
            if($childNode->nodeName == 'form')
            {
              $attributes = $childNode->attributes;
              if(!is_null($attributes))
              {
                foreach ($attributes as $index=>$attr)
                {
                  if($attr->name == "action")
                  {
                    $action_parts = explode('/', $attr->value);
                    $next = false;
                    foreach($action_parts as $action_part)
                    {
                      if($next)
                      {
                        $offer_id = $action_part;
                        break;
                      }
                      if($action_part == 'product')
                      {
                        $next = true;
                      }
                    }
                  }
                }
              }
              break;
            }
          }
        }
      }
      
      $discount_percent = trim(strip_tags(Utils::subString($html,"<strong>Reducere</strong>","%</span>")));
      
      $images = $res->getElementsByTagName('img');
      for($i = 0; $i < $images->length; $i++)
      {
        $image = $images->item($i);
        $attributes = $image->attributes;
        if(!is_null($attributes))
        {
            $is_offer_image = false;
            foreach ($attributes as $index=>$attr)
            {
                if($attr->name == "class" && $attr->value == "offerimage")
                {
                  $is_offer_image = true;
                }
                if($is_offer_image)
                {
                  if($attr->name == "src")
                  {
                    $offer_images[] = $attr->value;
                  }
                }
            }
        }
      }
      
      if(count($offer_images))
      {
        $offer_images = array($offer_images[0]);
      }
      
      if($offer_id)
      {
        $left_time = Utils::subString($html,"counterbox".$offer_id,"</script>");
        $left_time = Utils::subString($left_time,"product_countdown('","',");
      }
      
      if($debug)
      {
        var_dump($url);
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_images);
        var_dump($left_time);
        var_dump($discount_percent);
        /*
        if(!$offer_id)
        {
          var_dump($html);
        }
        */
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'fundeal.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($discount_percent);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }

    /*
    public static function parseFundealForExtraOffers($url)
    {
      $urls = array(
        'http://www.fundeal.biz/fundeal_ro/side/block1.php',
        'http://www.fundeal.biz/fundeal_ro/side/block2.php',
        'http://www.fundeal.biz/fundeal_ro/side/block3.php',
        'http://www.fundeal.biz/fundeal_ro/side/block4.php',
        'http://www.fundeal.biz/fundeal_ro/side/block5.php',
        'http://www.fundeal.biz/fundeal_ro/side/block6.php',
      );
      
      $extra_offers = array();
      
      foreach($urls as $url)
      {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html = curl_exec($ch);
        curl_close($ch);
        
        $res = new DomDocument('1.0', 'utf8');
        $res->validateOnParse = true;
        @$res->loadHTML($html);
        
        $links = $res->getElementsByTagName('a');
        
        foreach($links as $link)
        {
          if($link->getAttribute('href'))
          {
            $offer_url = str_replace('fundeal.ro','www.fundeal.ro',$link->getAttribute('href'));
            $extra_offers[$offer_url] = $offer_url;
          }
        }
      }
      
      return $extra_offers;
      
      $b = new sfWebBrowser();
      $b->get($url);
      $res = $b->getResponseDom();
      $h3s = $res->getElementsByTagName('h3');
      
      for($i = 0; $i < $h3s->length; $i++)
      {
        $h3 = $h3s->item($i);
        $attributes = $h3->attributes;
        if(!is_null($attributes))
        {
          $is_extra_offer = false;
          foreach ($attributes as $index=>$attr)
          {
            if($attr->name == "id" && $attr->value == "extra-desc")
            {
              $is_extra_offer = true;
            }
            if($is_extra_offer)
            {
              $childNodes = $h3->childNodes;
              for($j = 0; $j < $childNodes->length; $j++)
              {
                $childNode = $childNodes->item($j);
                if($childNode->nodeName == 'a')
                {
                  $child_attributes = $childNode->attributes;
                  if(!is_null($child_attributes))
                  {
                    foreach ($child_attributes as $index=>$attr)
                    {
                      if($attr->name == "href")
                      {
                        $extra_offers[] = $attr->value;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    */

    public static function parseKuponiada($url)
    {
      $offers = array();
      $offers[] = SiteParser::parseKuponiadaOffer($url);
      
      $extra_offers = SiteParser::parseKuponiadaForExtraOffers($url);
      if($extra_offers[0])
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseKuponiadaForExtraOffers($extra_offers[0]));
      }
      
      $extra_offers = array_unique($extra_offers);
       
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseKuponiadaOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseKuponiadaForExtraOffers($url)
    {
      $html = file_get_contents($url);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'extra_deal')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                $extra_offers[] = $childNode->getAttribute('href');
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseKuponiadaOffer($url)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $price = null;
      $discount_percent = null;
      $offer_id = null;
      
      $html = file_get_contents($url);
      $html = str_replace(array('<!--', '//-->'), array('', ''), $html);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h1s = $res->getElementsByTagName('h1');
      if($h1s->length)
      {
        $h1 = $h1s->item(0);
        $childNodes = $h1->childNodes;
        for($j = 0; $j < $childNodes->length; $j++)
        {
          $childNode = $childNodes->item($j);
          if($childNode->nodeName == 'a')
          {
            $offer_id = Utils::subString($childNode->getAttribute('onclick'), 'deal_order_post(', ');');
            $offer_title = trim($childNode->nodeValue);
          }
        }
      }
              
      $divs = $res->getElementsByTagName('div');
      
      for($i = 0; $i < $divs->length; $i++)
      {
        $div = $divs->item($i);
        if($div->getAttribute('class') == "descriere")
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == "p")
              {
                $offer_description = strip_tags(Utils::domNodeContent($childNode), '<br>');
                break;
              }
            }
          }
        }
      }
      
      $div = $res->getElementById('slider_object');
      if($div)
      {
        $childNodes = $div->childNodes;
        for($j = 0; $j < $childNodes->length; $j++)
        {
          $childNode = $childNodes->item($j);
          if($childNode->nodeName == 'img')
          {
            $attributes = $childNode->attributes;
            if(!is_null($attributes))
            {
              foreach ($attributes as $index=>$attr)
              {
                if($attr->name == "src")
                {
                  $offer_images[] = $attr->value;
                }
              }
            }
          }
        }
      }
      
      $div = $res->getElementById('price_data');
      if($div)
      {
        $childNodes = $div->childNodes;
        for($i = 0; $i < $childNodes->length; $i++)
        {
          $childNode = $childNodes->item($i);
          if($childNode->nodeName == 'div')
          {
            $childNodes2 = $childNode->childNodes;
            for($j = 0; $j < $childNodes2->length; $j++)
            {
              $childNode2 = $childNodes2->item($j);
              if($childNode2->nodeName == 'div')
              {
                $childNodes3 = $childNode2->childNodes;
                for($k = 0; $k < $childNodes3->length; $k++)
                {
                  $childNode3 = $childNodes3->item($k);
                  if($childNode3->nodeName == 'strong')
                  {
                    if(!$price)
                    {
                      $price = floatval(preg_replace('/[^0-9.,]/','',$childNode3->nodeValue));
                    }
                    else
                    {
                      if(strpos($childNode3->nodeValue, '%'))
                      {
                        $discount_percent = floatval(preg_replace('/[^0-9.,]/','',$childNode3->nodeValue));
                      }
                    }                    
                  }
                }
              }
            }
          }
        }
      }
      
      if(!$discount_percent)
      {
        $discount_percent = 0;
      }
      
      $scripts = $res->getElementsByTagName('script');
      foreach($scripts as $script)
      {
        if($pos = strpos($script->nodeValue, 'var kdc_servertime = '))
        {
          $part1 = substr($script->nodeValue, $pos + 21);
          $pos2 = strpos($part1, ';');
          $time1 = substr($part1, 0, $pos2);
        }
        if($pos = strpos($script->nodeValue, 'var kdc_dealendtime = '))
        {
          $part1 = substr($script->nodeValue, $pos + 22);
          $pos2 = strpos($part1, ';');
          $time2 = substr($part1, 0, $pos2);
        }
        
        if(isset($time1) && isset($time2))
        {
          $left_time = $time2 - $time1;
        }
      }
     
      /*
      var_dump($offer_id);
      var_dump($offer_title);
      var_dump($offer_description);
      var_dump($offer_images);
      var_dump($left_time);
      var_dump($price);
      var_dump($full_price);
      */
     
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'kuponiada.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(utf8_decode($offer_title));
        $offer->setDescription(utf8_decode($offer_description));
        $offer->setDiscountPercent($discount_percent);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseZumzi($url)
    {
      $offers = array();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $xml = new SimpleXMLElement($html);
      
      foreach($xml->channel->item as $deal)
      {
        $offers[] = SiteParser::parseZumziOffer($deal);
      }
        
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseZumziOffer($xml)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
       
      $offer_title = (string)$xml->title;
      $offer_url = (string)$xml->link;
      $offer_description = trim(strip_tags((string)$xml->description));
      $offer_id = Utils::subString((string)$xml->link, 'http://www.zumzi.ro/', '/');
      $offer_discount = (string)$xml->reduction;
      $left_time = strtotime((string)$xml->endDate) - time();
      $offer_images[] = Utils::subString((string)$xml->description, '<img src="' , '"');
      
      $cities = array();
      if($city = (string)$xml->city)
      {
        $cities = CityPeer::guessCitiesByName($city);
      }
      /*
      var_dump($offer_id);
      var_dump((string)$xml->city);
      foreach($cities as $city)
      {
        var_dump($city->getId());
        var_dump($city->getName());
      }
      var_dump('---');
      */
      if(!$offer_discount)
      {
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if($p > $offer_discount)
              {
                $offer_discount = $p;
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if(count($cities) && $offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'zumzi.ro:'.$offer_id;

        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }

        foreach($offer->getOfferCitys() as $offer_city)
        {
          try
          {
            $offer_city->delete();
          }
          catch(Exception $e)
          {
            // var_dump($e->getMessage());
          }
        }
        
        foreach($cities as $city)
        {
          $offer_city = new OfferCity();
          $offer_city->setCityId($city->getId());
          $offer_city->setOfferId($offer->getId());
          $offer_city->save();
        }
        
        return $offer;
      }
      
      return null;
    }
 
    public static function parseGroupon($url)
    {
      $offers = array();
      $offers[] = SiteParser::parseGrouponOffer($url);
      
      $extra_offers = SiteParser::parseGrouponForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseGrouponOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
 
    public static function parseGrouponForExtraOffers($url)
    {
      $html = file_get_contents($url);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'extraDealDescription')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'h3')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'a')
                    {
                      $extra_offers[] = 'http://www.groupon.ro'.$childNode2->getAttribute('href');
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseGrouponOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      $html = file_get_contents($url);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      /*
      $div = $res->getElementById('contentDealTitle');
      if($div->hasChildNodes())
      {
        foreach($div->childNodes as $childNode)
        {
          if($childNode->nodeName == 'h1')
          {
            $offer_title = $childNode->nodeValue;
          }
        }
      }
      */
      
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));
      
      $div = $res->getElementById('contentDealDescription');
      if($div->hasChildNodes())
      {
        foreach($div->childNodes as $childNode)
        {
          if($childNode->nodeName == 'div')
          {
            $offer_description = trim(Utils::subString($childNode->nodeValue,'Caracteristici'));
            //$offer_description = str_replace(array("\r\n", "\n", "\r"), "", $offer_description);
            break;
          }
        }
      }
      
      if($input = $res->getElementById('dotdId'))
      {
        $offer_id = $input->getAttribute('value');
      }
      
      if($div = $res->getElementById('contentDealBuyBox'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'span' && $childNode->getAttribute('class') == 'price')
            {
              //$price = floatval(preg_replace('/[^0-9.,]/','',$childNode->nodeValue));
            }
            if($childNode->nodeName == 'table')
            {
              $offer_discount = floatval(preg_replace('/[^0-9.,%]/','',$childNode->nodeValue));
              //$full_price = round($price * 100 / (100 - $discount), 2);
            }
          }
        }
      }
      if(!$offer_discount)
      {
        $offer_discount = 0;
      }
      
      $buttons = $res->getElementsByTagName('button');
      foreach($buttons as $button)
      {
        if($button->getAttribute('type') == 'submit' && $button->getAttribute('class') == 'nobg')
        {
          if($button->hasChildNodes())
          {
            foreach($button->childNodes as $childNode)
            {
              if($childNode->nodeName == 'img')
              {
                $offer_images[] = $childNode->getAttribute('src');
              }
            }
          }
        }
      }
      
      $input = $res->getElementById('currentTimeLeft');
      $left_time = $input->getAttribute('value');
      $left_time = round($left_time / 1000);
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_images);
        var_dump($left_time);
        var_dump($offer_discount);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && !is_null($offer_discount))
      {
        $unique_id = 'groupon.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseGoldendeals($url)
    {
      $offers = array();
      $offers[] = SiteParser::parseGoldendealsOffer($url);
      
      $extra_offers = SiteParser::parseGoldendealsForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseGoldendealsOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
 
    public static function parseGoldendealsForExtraOffers($url)
    {
      $html = file_get_contents($url);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'otherDeal')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'h4')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'a')
                    {
                      $extra_offers[] = 'http://www.goldendeals.ro'.$childNode2->getAttribute('href');
                    }
                  }
                }
              }
            }
          }
        }
      }

      return $extra_offers;
    }
    
    public static function parseGoldendealsOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $price = null;
      $full_price = null;
      $offer_id = null;
      
      $html = file_get_contents($url);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h1s = $res->getElementsByTagName('h1');
      foreach($h1s as $h1)
      {
        if($h1->getAttribute('class') == 'noUnderline maintitle')
        {
          if($h1->hasChildNodes())
          {
            foreach($h1->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                $offer_title = $childNode->nodeValue;
                $offer_id = $childNode->getAttribute('href');
              }
            }
          }
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'containerRight')
        {
          $offer_description = trim($div->nodeValue);
          break;
        }
      }
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'buttonBuy')
        {
          $price = floatval(preg_replace('/[^0-9.,]/','',$link->nodeValue));
          break;
        }
      }
      
      $spans = $res->getElementsByTagName('span');
      foreach($spans as $span)
      {
        if($span->getAttribute('class') == 'price')
        {
          $full_price = floatval(preg_replace('/[^0-9.,]/','',$span->nodeValue));
          break;
        }
      }
       
      $div = $res->getElementById('image_canvas');
      if($div->hasChildNodes())
      {
        foreach($div->childNodes as $childNode)
        {
          if($childNode->nodeName == 'img')
          {
            $offer_images[] = $childNode->getAttribute('src');
          }
        }
      }
      
      $span = $res->getElementById('hours');
      
      $scripts = $res->getElementsByTagName('script');
      foreach($scripts as $script)
      {
        $day = Utils::subString($script->nodeValue, "'day':", ",");
        $month = Utils::subString($script->nodeValue, "'month':", ",");
        $year = Utils::subString($script->nodeValue, "'year':", ",");
        $hour = Utils::subString($script->nodeValue, "'hour':", ",");
        $min = Utils::subString($script->nodeValue, "'min':", ",");
        $sec = Utils::subString($script->nodeValue, "'sec':", "}");
        if($day && $month && $year && $hour && $min && $sec)
        {
          $left_time = strtotime($year.'-'.$month.'-'.$day.' '.$hour.':'.$min.':'.$sec) - time();
        }
      }
        
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_images);
        var_dump($left_time);
        var_dump($price);
        var_dump($full_price);
        //die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $price && $full_price)
      {
        $unique_id = 'goldendeals.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setPrice($price);
        $offer->setFullPrice($full_price);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseCoolbuy($url)
    {
      
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $offers = array();
      $offer = SiteParser::parseCoolbuyOffer('http://www.coolbuy.ro/');
      $offers[] = $offer;
      
      $extra_offers = SiteParser::parseCoolbuyForExtraOffers('http://www.coolbuy.ro/');

      foreach($extra_offers as $url)
      {
        $offer = SiteParser::parseCoolbuyOffer($url);
        $offers[] = $offer;
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
 
    public static function parseCoolbuyForExtraOffers($url)
    {
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'side_offer_right corner_right')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                $extra_offers[] = $childNode->getAttribute('href');
                break;
              }
            }
          }
        }
      }
       
      return $extra_offers;
    }
    
    public static function parseCoolbuyOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_id = null;
      $offer_url = null;
      $offer_discount = null;
      
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h1s = $res->getElementsByTagName('h1');
      foreach($h1s as $h1)
      {
        if($h1->hasChildNodes())
        {
          foreach($h1->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $offer_title = $childNode->nodeValue;
              $offer_url = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'offer_buy_button corner_all')
        {
          $offer_id = Utils::subString($link->getAttribute('href'),'http://www.coolbuy.ro/orders/add/');
          break;
        }
      }
      
      $offer_description = trim(strip_tags(Utils::subString($html, '<h2>Descriere</h2>', '<div')));
      
      /*
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'offer_desc')
        {
          $offer_description = trim($div->nodeValue);
          break;
        }
      }
      */
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'offer_buy_info corner_all')
        {
          $offer_discount = intval(trim($div->nodeValue));
          break;
        }
      }
       
      $imgs = $res->getElementsByTagName('img');
      foreach($imgs as $img)
      {
        if($img->getAttribute('class') == 'corner_tl')
        {
          $offer_images[] = $img->getAttribute('src');
          break;
        }
      }
      
      $left_time = Utils::subString($html, "var stop_timer_value =", ";");
        
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_images);
        var_dump($left_time);
        var_dump($offer_discount);
        var_dump($offer_url);
        die();
      }
      
      if($offer_id && $offer_title && $offer_url && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'coolbuy.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseTopdeals($url)
    {
      $offers = array();
      $offers[] = SiteParser::parseTopdealsOffer($url);
      
      $extra_offers = SiteParser::parseTopdealsForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseTopdealsOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
 
    public static function parseTopdealsForExtraOffers($url)
    {
      $html = file_get_contents($url);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $uls = $res->getElementsByTagName('ul');
      foreach($uls as $ul)
      {
        if($ul->getAttribute('class') == 'right_second_deal')
        {
          if($ul->hasChildNodes())
          {
            foreach($ul->childNodes as $childNode)
            {
              if($childNode->nodeName == 'li')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'a')
                    {
                      $extra_offers[] = 'http://www.topdeals.ro'.$childNode2->getAttribute('href');
                    }
                  }
                }
              }
            }
          }
        }
      }

      return $extra_offers;
    }
    
    public static function parseTopdealsOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $price = null;
      $full_price = null;
      $offer_id = null;
      
      $html = file_get_contents($url);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'deal')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'h1')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == '#text')
                    {
                      $offer_title = $childNode2->nodeValue;
                      break;
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'buy')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                $offer_id = Utils::subString($childNode->getAttribute('href'),'/topdeals/deal/buy/');
              }
            }
          }
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'deal_benefits')
        {
          $offer_description = trim(Utils::subString($div->nodeValue, 'Avantaje'));
        }
      }
      
      $dls = $res->getElementsByTagName('dl');
      foreach($dls as $dl)
      {
        if($dl->getAttribute('class') == 'rebate')
        {
          if($dl->hasChildNodes())
          {
            foreach($dl->childNodes as $childNode)
            {
              if($childNode->nodeName == 'dd')
              {
                $offer_discount = intval($childNode->nodeValue);
              }
            }
          }
        }
      }
      
      $lis = $res->getElementsByTagName('li');
      foreach($lis as $li)
      {
        if($li->getAttribute('class') == 'slide')
        {
          if($li->hasChildNodes())
          {
            foreach($li->childNodes as $childNode)
            {
              if($childNode->nodeName == 'img')
              {
                $offer_images[] = 'http://www.topdeals.ro'.$childNode->getAttribute('src');
              }
            }
          }
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'deal_rtime')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'ul')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'li')
                    {
                      if(!isset($hours))
                      {
                        $hours = intval($childNode2->nodeValue);
                        continue;
                      }
                      if(!isset($minutes))
                      {
                        $minutes = intval($childNode2->nodeValue);
                        continue;
                      }
                      if(!isset($seconds))
                      {
                        $seconds = intval($childNode2->nodeValue);
                        continue;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      if(isset($hours) && isset($minutes) && isset($seconds))
      {
        $left_time = $hours * 3600 + $minutes * 60 + $seconds;
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'topdeals.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl('http://www.topdeals.ro/topdeals/deal/view/'.$offer_id);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseNoi2($url)
    {
      $offers = array();
      $offers[] = SiteParser::parseNoi2Offer($url);
      
      $extra_offers = SiteParser::parseNoi2ForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseNoi2Offer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseNoi2ForExtraOffers($url)
    {
      return array();
      
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      if($pos = strpos($html,'<meta http-equiv="refresh" content="0;url='))
      {
        $part1 = substr($html, $pos + 42);
        if($pos = strpos($part1,'"'))
        {
          $url = substr($part1, 0, $pos);
          
          return SiteParser::parseZumziForExtraOffers($url);
        }
      }
       
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'button_detalii_sideoffer')
        {
          $extra_offers[] = 'http://www.zumzi.ro'.$link->getAttribute('href');
        }
      }

      return $extra_offers;
    }
    
    public static function parseNoi2Offer($url)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $price = null;
      $offer_discount = null;

      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      if($pos = strpos($html,'<meta http-equiv="refresh" content="0;url='))
      {
        $part1 = substr($html, $pos + 42);
        if($pos = strpos($part1,'"'))
        {
          $url = substr($part1, 0, $pos);
          
          return SiteParser::parseNoi2Offer($url);
        }
      }
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h2s = $res->getElementsByTagName('h2');
      foreach($h2s as $h2)
      {
        if($h2->nodeValue == 'Uite oferta din Bucuresti:')
        {
          return null;
        }
      }
      
      $offer_title = Utils::subString($html, 'Oferta curenta:</span>', '</h2>');
      
      $offer_description = trim(strip_tags(Utils::subString($html, 'Oferta pe scurt</h3>', '<p style="margin: 0px; padding: 0px;">
            Voucher valabil pana la:')));
      
      for($i = 1; $i <= 5; $i++)
      {
        if($div = $res->getElementById('offer_image_'.$i))
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'img')
              {
                $offer_images[] = $childNode->getAttribute('src');
              }
            }
          }
        }
      }
      
      if($div = $res->getElementById('left_hours'))
      {
        $left_hours = $div->nodeValue;
      }
      if($div = $res->getElementById('left_minutes'))
      {
        $left_minutes = $div->nodeValue;
      }
      if($div = $res->getElementById('left_seconds'))
      {
        $left_seconds = $div->nodeValue;
      }
      
      if(isset($left_hours) && isset($left_minutes) && isset($left_seconds))
      {
        $left_time = $left_hours * 3600 + $left_minutes * 60 + $left_seconds;
      }
      
      $tds = $res->getElementsByTagName('td');
      $i = 0;
      foreach($tds as $td)
      {
        $i++;
        if($i == 14)
        {
          $offer_discount = intval($td->nodeValue);
        }
      }

      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'button_buy')
        {
          $offer_id = Utils::subString($link->getAttribute('href'), '/offer/comanda/confirm/');
        }
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'oferta.noi2.ro:'.$offer_id;

        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parsePerfectdeals($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parsePerfectdealsOffer($url);
      
      /*
      $extra_offers = SiteParser::parseKuponiadaForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseKuponiadaOffer($url);
      }
      */
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parsePerfectdealsOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      $html = file_get_contents($url);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h1s = $res->getElementsByTagName('h1');
      foreach($h1s as $h1)
      {
        $offer_title = $h1->nodeValue;
        break;
      }
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h1 class="offer_h1">Avantaje</h1>','</div>')));
      
      $inputs = $res->getElementsByTagName('input');
      foreach($inputs as $input)
      {
        if($input->getAttribute('name') == 'offer_id')
        {
          $offer_id = $input->getAttribute('value');
        }
      }
 
      if($div = $res->getElementById('discount_percent_div'))
      {
        $offer_discount = intval($div->nodeValue);
      }
      
      $imgs = $res->getElementsByTagName('img');
      foreach($imgs as $img)
      {
        if($img->getAttribute('class') == 'offerimage')
        {
          $offer_images[] = $img->getAttribute('src');
        }
      }
      
      $left_time = Utils::subString($html,'dd_countdown(',')');
      
      $offer_url = Utils::subString($html,'http://www.facebook.com/sharer.php?u=','"');
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_url);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'perfectdeals.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName($offer_title);
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseGetadeal($url, $debug = false)
    {
      $offers = array();
      
      $extra_offers = SiteParser::parseGetadealForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseGetadealOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseGetadealForExtraOffers($url)
    {
      $html = file_get_contents($url);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if(strpos($link->getAttribute('href'),'/offer.php?offer_id=') === 0)
        {
          $extra_offers[] = 'http://www.getadeal.ro'.$link->getAttribute('href');
        }
      }
      
      $extra_offers = array_unique($extra_offers);
      
      return $extra_offers;
    }
    
    public static function parseGetadealOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      $html = file_get_contents($url);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 class="title" style="font-size:20px; color:#222; padding-bottom:15px;">','</h1>')));
      $offer_title = str_replace('&nbsp;', '', $offer_title);

      $offer_description = trim(strip_tags(Utils::subString($html,'<p class="offer_detalii">','</p>')));
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if(strpos($link->getAttribute('href'),'/buy.php?offer_id=') === 0)
        {
          $offer_id = Utils::subString($link->getAttribute('href'),'/buy.php?offer_id=');
          break;
        }
      }
      
      $offer_discount = intval(Utils::subString($html,'<span class="color_green">Discount:</span>','%'));
       
      
      if($ul = $res->getElementById('the_fade'))
      {
        foreach($ul->childNodes as $childNode)
        {
          if($childNode->nodeName == 'li' && $childNode->hasChildNodes())
          {
            foreach($childNode->childNodes as $childNode2)
            {
              if($childNode2->nodeName == 'img')
              {
                $offer_images[] = 'http://www.getadeal.ro'.$childNode2->getAttribute('src');
              }
            }
          }
        }
      }

      if($td = $res->getElementById('left_hours'))
      {
        $left_hours = $td->nodeValue;
      }
      if($td = $res->getElementById('left_minutes'))
      {
        $left_minutes = $td->nodeValue;
      }
      if($td = $res->getElementById('left_seconds'))
      {
        $left_seconds = $td->nodeValue;
      }
      
      if(isset($left_hours) && isset($left_minutes) && isset($left_seconds))
      {
        $left_time = $left_hours * 3600 + $left_minutes * 60 + $left_seconds;
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'getadeal.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseReddeal($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseReddealOffer($url);
      
      $extra_offers = SiteParser::parseReddealForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseReddealOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseReddealForExtraOffers($url)
    {
      $html = file_get_contents($url);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'side-deal' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'a')
                {
                  $extra_offers[] = 'http://www.reddeal.ro'.$childNode2->getAttribute('href');
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseReddealOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      $html = file_get_contents($url);
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 class="title">','</h1>')));
      if(strpos($offer_title, 'Deal-ul zilei:') !== false)
      {
        $offer_title = trim(Utils::subString($offer_title,'Deal-ul zilei:'));
      }
      
      $offer_description = trim(strip_tags(str_replace('<br />',"\n",Utils::subString($html,'<h2>Detalii Deal</h2>','</div>'))));
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'button')
        {
          $offer_id = Utils::subString($link->getAttribute('href'),'/buy/');
          break;
        }
      }
      
      $dls = $res->getElementsByTagName('dl');
      foreach($dls as $dl)
      {
        if($dl->getAttribute('class') == 'deal-discount clearfix')
        {
          if($dl->hasChildNodes())
          {
            foreach($dl->childNodes as $childNode)
            {
              if($childNode->nodeName == 'dd')
              {
                $offer_discount = intval($childNode->nodeValue);
                break;
              }
            }
          }
        }
      }
      
      if(!$offer_discount)
      {
        $offer_discount = 0;
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if(intval($p) > $offer_discount && intval($p) < 100)
              {
                $offer_discount = intval($p);
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'section2')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'img')
              {
                $offer_images[] = 'http://www.reddeal.ro'.$childNode->getAttribute('src');
                break;
              }
            }
          }
        }
      }
      
      $h1s = $res->getElementsByTagName('h1');
      foreach($h1s as $h1)
      {
        if($h1->getAttribute('class') == 'title')
        {
          if($h1->hasChildNodes())
          {
            foreach($h1->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                $offer_url = 'http://www.reddeal.ro'.$childNode->getAttribute('href');
                break;
              }
            }
          }
        }
      }
      
      $left_time = intval(Utils::subString($html,'<span class="js-time hide">','</span>'));
         
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_url);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'reddeal.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseProfitamro($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseProfitamroOffer($url);
      
      $extra_offers = SiteParser::parseProfitamroForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseProfitamroOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseProfitamroForExtraOffers($url)
    {
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
      curl_setopt($ch, CURLOPT_HEADER, true);
      //curl_setopt($ch, CURLOPT_VERBOSE, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'side-block' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeType == XML_ELEMENT_NODE && $childNode->getAttribute('class') == 'block-content' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeType == XML_ELEMENT_NODE && $childNode2->getAttribute('class') == 'midpix' && $childNode2->hasChildNodes())
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {
                    if($childNode3->nodeType == XML_ELEMENT_NODE && $childNode3->getAttribute('class') == 'midtxt' && $childNode3->hasChildNodes())
                    {
                      foreach($childNode3->childNodes as $childNode4)
                      {
                        if($childNode4->nodeName == 'a')
                        {
                          $extra_offers[] = $childNode4->getAttribute('href');
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseProfitamroOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      // $html = file_get_contents($url);
      
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
      curl_setopt($ch, CURLOPT_HEADER, true);
      //curl_setopt($ch, CURLOPT_VERBOSE, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $offer_title = trim(Utils::subString($html,'<h1 style="line-height: 30px;">Oferta zilei: <span>','</span>'));
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'highlights')
        {
          $offer_description = trim(Utils::subString($div->nodeValue,'Beneficii'));
        }
      }
            
      if($link = $res->getElementById('scriptbuynow'))
      {
          $offer_id = Utils::subString($link->getAttribute('href'),'cid=');
      }
      
      $offer_discount = trim(Utils::subString($html,'Reducere&nbsp;','%'));
      
      $div = $res->getElementById('loopedslider');
      foreach($div->childNodes as $childNode)
      {
        if($childNode->nodeName == 'div' && $childNode->hasChildNodes())
        {
          foreach($childNode->childNodes as $childNode2)
          {
            if($childNode2->nodeName == 'div' && $childNode2->hasChildNodes())
            {
              foreach($childNode2->childNodes as $childNode3)
              {
                if($childNode3->nodeName == 'img')
                {
                  $offer_images[] = $childNode3->getAttribute('src');
                }
              }
            }
          }
        }
      }
      
      $year = Utils::subString($html, 'var yyear   = "', '";');
      $month = Utils::subString($html, 'var ymonth  = "', '";');
      $day = Utils::subString($html, 'var yday    = "', '";');
      $hour = Utils::subString($html, 'var yhour   = "', '";');
      $minute = Utils::subString($html, 'var yminute = "', '";');
      $sec = Utils::subString($html, 'var ysec    = "', '";');
      
      $left_time = $year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$sec;
      $left_time = strtotime($left_time) - time();
       
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'profitam.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseIdeals($url, $debug = false)
    {
      $offers = array();
      
      $extra_offers = SiteParser::parseIdealsForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseIdealsOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseIdealsForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h2s = $res->getElementsByTagName('h2');
      foreach($h2s as $h2)
      {
        if($h2->hasChildNodes())
        {
          foreach($h2->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseIdealsOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h2>','</h2>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<div class="descriere_oferta">','</div>')));
      
      $offer_id = trim(strip_tags(Utils::subString($html,'http://www.ideals.ro/oferte/timp/\' + ',',')));
      
      $offer_discount = trim(Utils::subString($html,'<li>reducere <p>','%'));
      
      if($offer_discount >= 100)
      {
        $offer_discount = '';
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'oferta')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'img')
                    {
                      $offer_images[] = $childNode2->getAttribute('src');
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      $left_time = file_get_contents('http://www.ideals.ro/oferte/timp/'.$offer_id);
            
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'ideals.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseCitydays($url, $debug = false)
    {
      $offers = array();
      
      $extra_offers = SiteParser::parseCitydaysForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseCitydaysOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseCitydaysForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'city-offer-box clearfix' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = 'http://www.citydays.ro/'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseCitydaysOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      if($div = $res->getElementById('main-content'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'h2')
            {
              $offer_title = $childNode->nodeValue;
              break;
            }
          }
        }
      }
      
      $offer_description = trim(strip_tags(str_replace(array("<br />"), "\n", Utils::subString($html,'</h3>','</div>'))));
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'buy-now')
        {
          $offer_id = Utils::subString($link->getAttribute('href'), "/buy/");
        }
      }
      
      $offer_price = null;
      $offer_full_price = null;
      $ps = $res->getElementsByTagName('p');
      foreach($ps as $p)
      {
        if($p->getAttribute('class') == 'price')
        {
          $offer_price = floatval(str_replace(',', '', $p->nodeValue));
        }
        
        if($p->getAttribute('class') == 'old-price')
        {
          $offer_full_price = floatval(str_replace(',', '', $p->nodeValue));
        }
      }
      
      if($offer_price && $offer_full_price)
      {
        $offer_discount = round(100 - ($offer_price * 100 / $offer_full_price));
      }
      
      if($div = $res->getElementById('carousel'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'ul')
            {
              if($childNode->hasChildNodes())
              {
                foreach($childNode->childNodes as $childNode2)
                {
                  if($childNode2->nodeName == 'li')
                  {
                    if($childNode2->hasChildNodes())
                    {
                      foreach($childNode2->childNodes as $childNode3)
                      {
                        if($childNode3->nodeName == 'img')
                        {
                          $offer_images[] = 'http://www.citydays.ro'.$childNode3->getAttribute('src');
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

      $left_time = strtotime(Utils::subString($html, 'cd.TargetDate = "', '";')) - time();
            
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'citydays.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseEredus($url, $debug = false)
    {
      $offers = array();
      //$offers[] = SiteParser::parseEredusOffer($url, $debug);
      
      $extra_offers = SiteParser::parseEredusForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseEredusOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseEredusForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'deal-title' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseEredusOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(Utils::subString(strip_tags(Utils::subString($html,'<h2 id="deal-name">','</h2>')), 'Oferta zilei:'));
      if(!$offer_title)
      {
        $offer_title = trim(Utils::subString(strip_tags(Utils::subString($html,'<h2 id="deal-name">','</h2>')), 'Oferta Zilei:'));
      }
      
      $offer_description = trim(Utils::subString(strip_tags(Utils::subString($html,'<div id=\'des-left\'>','</div>')), 'Avantaje'));

      if($div = $res->getElementById('buy-btn'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $offer_id = Utils::subString($childNode->getAttribute('href'), "http://e-redus.ro/confirmare/?deal=");
            }
          }
        }
      }
      
      if($div = $res->getElementById('deal-name'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $offer_url = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      if($div = $res->getElementById('buy-ind'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'p')
            {
              $offer_discount = Utils::subString($childNode->nodeValue, "Redus", "%");
              if($offer_discount)
              {
                break;
              }
            }
          }
        }
      }
      
      if($div = $res->getElementById('time-count'))
      {
        $left_time = $div->getAttribute('class');
      }
      
      if($div = $res->getElementById('buy-img'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'ul')
            {
              if($childNode->hasChildNodes())
              {
                foreach($childNode->childNodes as $childNode2)
                {
                  if($childNode2->nodeName == 'li')
                  {
                    if($childNode2->hasChildNodes())
                    {
                      foreach($childNode2->childNodes as $childNode3)
                      {
                        if($childNode3->nodeName == 'img')
                        {
                          $offer_images[] = $childNode3->getAttribute('src');
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      if(!count($offer_images))
      {
        if($div = $res->getElementById('deal-right'))
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'img')
              {
                $offer_images[] = $childNode->getAttribute('src');
              }
            }
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_url);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'e-redus.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseBestdealz($url, $debug = false)
    {
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $offers = array();
      
      $extra_offers = SiteParser::parseBestdealzForExtraOffers('http://www.bestdealz.ro');
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseBestdealzOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseBestdealzForExtraOffers($url)
    {
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
      $html = curl_exec($ch);
      curl_close($ch);
       
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if(strpos($link->getAttribute('href'), "/oferta/") === 0)
        {
          $extra_offers[$link->getAttribute('href')] = 'http://www.bestdealz.ro'.$link->getAttribute('href');
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseBestdealzOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_full_price = null;
      $offer_discount_price = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      $ckfile = sfConfig::get('sf_upload_dir').'/cookies.txt';
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
      $html = curl_exec($ch);
      curl_close($ch);
        
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(Utils::subString(strip_tags(Utils::subString($html,'<h1>','</h1>')), 'Oferta de azi:'));
      
      $offer_description = trim(Utils::subString(strip_tags(str_replace("<br />", "\n", Utils::subString($html,'<div class="deal-box-modbgd">','</div>'))), 'Oferta pe scurt'));
      
      $offer_id = Utils::subString($html,'<a class="deal-today-link" href="/oferta/','/');
      
      $offer_url = 'http://www.bestdealz.ro'.Utils::subString($html,'<a class="deal-today-link" href="','"');
     
      $tables = $res->getElementsByTagName('table');
      $trs = 0;
      $tds = 0;
      foreach($tables as $table)
      {
        if($table->getAttribute('class') == 'deal-discount')
        {
          if($table->hasChildNodes())
          {
            foreach($table->childNodes as $childNode)
            {
              if($childNode->nodeName == 'tr')
              {
                $trs++;
                if($trs == 2 && $childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'td')
                    {
                      $tds++;
                      if($tds == 1)
                      {
                        $offer_full_price = floatval($childNode2->nodeValue);
                      }
                      if($tds == 2)
                      {
                        $offer_discount_price  = floatval($childNode2->nodeValue);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      if($offer_full_price && $offer_discount_price)
      {
        $offer_price = $offer_full_price - $offer_discount_price;
        $offer_discount = round(100 - ($offer_price * 100 / $offer_full_price));
      }
      
      if(is_null($offer_discount))
      {
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if(intval($p) > $offer_discount)
              {
                $offer_discount = intval($p);
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      if($ul = $res->getElementById('counter'))
      {
        if($ul->hasChildNodes())
        {
          foreach($ul->childNodes as $childNode)
          {
            if($childNode->nodeName == 'li')
            {
              if(!isset($hours))
              {
                $hours = $childNode->nodeValue;
                continue;
              }
              if(!isset($minutes))
              {
                $minutes = $childNode->nodeValue;
                continue;
              }
              if(!isset($seconds))
              {
                $seconds = $childNode->nodeValue;
                continue;
              }
            }
          }
        }
      }
      
      if(isset($hours) && isset($minutes) && isset($seconds))
      {
        if(strpos($hours, 'zile'))
        {
          $left_time = $hours * 3600 * 24 + $minutes * 60 * 60 + $seconds * 60;
        }
        else
        {
          $left_time = $hours * 3600 + $minutes * 60 + $seconds;
        }
      }
      
      if($div = $res->getElementById('team_images'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div')
            {
              if($childNode->hasChildNodes())
              {
                foreach($childNode->childNodes as $childNode2)
                {
                  if($childNode2->nodeName == 'ul')
                  {
                    if($childNode2->hasChildNodes())
                    {
                      foreach($childNode2->childNodes as $childNode3)
                      {
                        if($childNode3->nodeName == 'li')
                        {
                          if($childNode3->hasChildNodes())
                          {
                            foreach($childNode3->childNodes as $childNode4)
                            {
                              if($childNode4->nodeName == 'img')
                              {
                                $offer_images[] = $childNode4->getAttribute('src');
                              }
                            }
                          }
                        }
                      }
                    }
                  }                  
                }
              }
            }
          }
        }
      }
      
      if(!count($offer_images))
      {
        if($div = $res->getElementById('team_images'))
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'img')
              {
                $offer_images[] = $childNode->getAttribute('src');
              }
            }
          }
        }
      }
      
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_url);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'bestdealz.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseQupon($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseQuponOffer($url, $debug);
      
      $extra_offers = SiteParser::parseQuponForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseQuponOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseQuponForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'altaOferta' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
         
      return $extra_offers;
    }
    
    public static function parseQuponOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<span id="textOfertaZilei">','</span>')));
      
      $offer_description = trim(Utils::subString(strip_tags(Utils::subString($html,'<div id="qupon_stie_ca">','</div>')), 'Qupon stie ca'));
      
      $offer_id = md5($offer_title);
      
      if($div = $res->getElementById('Reducere'))
      {
        $offer_discount = intval($div->nodeValue);
      }
      
      $day = trim(Utils::subString($html, "'day':", ","));
      $month = trim(Utils::subString($html, "'month':", ","));
      $year = trim(Utils::subString($html, "'year':", ","));
      $hour = trim(Utils::subString($html, "'hour':", ","));
      $min = trim(Utils::subString($html, "'min':", ","));
      $sec = trim(Utils::subString($html, "'sec':", "}"));
      
      $left_time = strtotime($year."-".$month."-".$day." ".$hour.':'.$min.":".$sec) - time();
        
      if($images = trim(Utils::subString($html, "imagearray: [", "]]")))
      {
        $parts = explode("],", $images);
        foreach($parts as $part)
        {
          if($part = trim(str_replace(array('[', '"', ','), "", $part)))
          {
            $offer_images[] = $part;
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'qupon.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseCrazydeal($url, $debug = false)
    {
      $offers = array();
      
      $extra_offers = SiteParser::parseCrazydealForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseCrazydealOffer($url);
      }
      
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseCrazydealForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);

      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      $divs = $res->getElementsByTagName('div');
      
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'ofertaactiva' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'titleactive' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'h2' && $childNode2->hasChildNodes())
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {
                    if($childNode3->nodeName == 'a')
                    {
                      $extra_offers[] = $childNode3->getAttribute('href');
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseCrazydealOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 class="title">','</h1>')));
                  
      //$offer_description = str_replace(array("\t","&nbsp;"), "", trim(strip_tags(Utils::subString($html,'<h3>Beneficii</h3>','</div>'))));
      //$offer_description = str_replace("\r\n\r\n", "\r\n", $offer_description);
      $offer_description = trim(strip_tags(Utils::subString($html,'<h2>Descrierea ofertei</h2>','</div>')));
      
      $offer_id = Utils::subString($html,'http://www.crazydeal.ro/payment/order/','"');
      
      $offer_discount = Utils::subString($html,'<div class="redm pad3">','%');
      $offer_discount = Utils::subString($offer_discount,'-');
      if(!$offer_discount)
      {
        preg_match('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          $offer_discount = intval($matches[0]);
        }
      }
      
      $left_time = trim(Utils::subString($html,'targetDate: {','omitWeeks:'));
      $parts = explode(',',$left_time);
      $y = $m = $d = $h = $i = $s = null;
      foreach($parts as $part)
      {
        $part = intval(trim(Utils::subString($part, ':')));
        if(is_null($d))
        {
          $d = $part;
        }
        elseif(is_null($m))
        {
          $m = $part;
        }
        elseif(is_null($y))
        {
          $y = $part;
        }
        elseif(is_null($h))
        {
          $h = $part;
        }
        elseif(is_null($i))
        {
          $i = $part;
        }
        elseif(is_null($s))
        {
          $s = $part;
        }
      }
      $left_time = strtotime($y.'-'.$m.'-'.$d.' '.$h.':'.$i.':'.$s) - time();
        
      $divs = $res->getElementsByTagName('div');
      
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'poza' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'img')
            {
              $offer_images[$childNode->getAttribute('src')] = $childNode->getAttribute('src');
            }
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && !is_null($offer_discount))
      {
        $unique_id = 'crazydeal.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseFungroup($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseFungroupOffer($url, $debug);
      
      $extra_offers = SiteParser::parseFungroupForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseFungroupOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseFungroupForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'promo_right' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'promo_img' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'a')
                {
                  $extra_offers[] = 'http://www.fungroup.ro'.$childNode2->getAttribute('href');
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseFungroupOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h2 class="ntitle">','</h2>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h3>avantaje</h3>','</div>')));
      
      $offer_id = Utils::subString($html,'href="/?page=cart&add=','"');
      
      $offer_discount = intval(strip_tags(Utils::subString($html,'<div class="app_procent">','<div'))) * -1;
      
      $left_time = Utils::subString($html,'<body Onload="dd_countdown(',')');
        
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'slideShow')
        {
          if($div->hasChildNodes())
          {
            foreach($div->childNodes as $childNode)
            {
              if($childNode->nodeName == 'a')
              {
                if($childNode->hasChildNodes())
                {
                  foreach($childNode->childNodes as $childNode2)
                  {
                    if($childNode2->nodeName == 'img')
                    {
                      $offer_images[] = 'http://www.fungroup.ro'.$childNode2->getAttribute('src');
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'fungroup.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parsePandoria($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parsePandoriaOffer($url, $debug);
      
      $extra_offers = SiteParser::parsePandoriaForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parsePandoriaOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parsePandoriaForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      if($div = $res->getElementById('oferta-altele'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'ul' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'li' && $childNode2->hasChildNodes())
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {
                    if($childNode3->nodeName == 'a')
                    {
                      $extra_offers[] = $childNode3->getAttribute('href');
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parsePandoriaOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(Utils::subString(strip_tags(Utils::subString($html,'<h1>','</h1>')), '% reducere: '));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'Descriere oferta','</div>')));
      
      $offer_id = Utils::subString($html,'http://www.pandoria.ro/ccart/index/','"');

      $offer_discount = trim(strip_tags(Utils::subString($html,'<ul id="oferta-pricing-details">','</ul>')));
      $offer_discount = Utils::subString($offer_discount, '', '%');
      
      $left_time = Utils::subString($html,'dateFuture = new Date(',')');
      $parts = explode(',', $left_time);
      if(count($parts) == 7)
      {
        $left_time = strtotime($parts[0].'-'.($parts[1]+1).'-'.$parts[2].' '.$parts[3].':'.$parts[4].':'.$parts[5]) - time();
      }
      else
      {
        $left_time = null;
      }
        
      $imgs = $res->getElementsByTagName('img');
      foreach($imgs as $img)
      {
        if($img->getAttribute('class') == 'description-image')
        {
          $offer_images[] = $img->getAttribute('src');
        }
      }
       
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'pandoria.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseDealfever($url, $debug = false)
    {
      $offers = array();
      $offers = array_merge($offers, SiteParser::parseDealfeverOffer($url, $debug));
      
      $extra_offers = SiteParser::parseDealfeverForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers = array_merge($offers, SiteParser::parseDealfeverOffer($url));
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseDealfeverForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $spans = $res->getElementsByTagName('span');
      foreach($spans as $span)
      {
        if($span->getAttribute('class') == 'see_buy' && $span->hasChildNodes())
        {
          foreach($span->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = 'http://www.dealfever.ro'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseDealfeverOffer($url, $debug = false)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $urls = array();
      $urls[] = $url;
      $offers = array();
      
      $h1s = $res->getElementsByTagName('h1');
      foreach($h1s as $h1)
      {
        if($h1->hasChildNodes())
        {
          foreach($h1->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $urls[] = 'http://www.dealfever.ro'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      foreach($urls as $url)
      {
        $offer_title = null;
        $offer_description = null;
        $offer_images = array();
        $left_time = null;
        $offer_discount = null;
        $offer_id = null;
        $offer_url = null;
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html = curl_exec($ch);
        curl_close($ch);
        
        $res = new DomDocument('1.0', 'utf8');
        $res->validateOnParse = true;
        @$res->loadHTML($html);
        
        $offer_title = trim(strip_tags(Utils::subString($html,'<h1 id="deal_title">','</h1>')));
        
        $offer_description = trim(strip_tags(Utils::subString($html,'<div class="terms">','</div>')));
        $offer_description = trim(strip_tags(Utils::subString($offer_description,'Beneficii')));
        
        $offer_id = Utils::subString($html,'/checkout?did=','"');
        
        $offer_discount = trim(strip_tags(Utils::subString($html,'<td class="center padding">Reducere<br />','</td>')));
        $offer_discount = Utils::subString($offer_discount, '', '%');

        $left_time = Utils::subString($html,'"adeal_countdown":',',');
        
        $imgs = $res->getElementsByTagName('img');
        foreach($imgs as $img)
        {
          if($img->getAttribute('class') == 'imagecache imagecache-deal imagecache-default imagecache-deal_default')
          {
            $offer_images[] = $img->getAttribute('src');
          }
        }
        
        if($debug)
        {
          var_dump($offer_id);
          var_dump($offer_title);
          var_dump($offer_description);
          var_dump($offer_discount);
          var_dump($offer_images);
          var_dump($left_time);
        }
      
        if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
        {
          $unique_id = 'dealfever.ro:'.$offer_id;
          $is_new = false;
          if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
          {
            $offer = new Offer();
            $is_new = true;
          }
          $offer->setUniqueId($unique_id);
          $offer->setName(html_entity_decode($offer_title));
          $offer->setDescription($offer_description);
          $offer->setDiscountPercent($offer_discount);
          $offer->setUrl($url);
          $offer->setStartTime(date('Y-m-d 00:00:00', time()));
          $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
          
          $offer->save();
          
          if($is_new)
          {
            foreach($offer_images as $image)
            {
              if($image)
              {
                $offer_image = new OfferImage();
                $offer_image->setOfferId($offer->getId());
                $offer_image->setUrl($image);
                $offer_image->save();
                $offer_image->setImage($offer_image->getId().'.jpg');
                if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
                {
                  $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                  unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                  if($image_check)
                  {
                    $offer_image->save();
                    break;
                  }
                  else
                  {
                    $offer_image->delete();
                  }
                }
                else
                {
                  $offer_image->delete();
                }
              }
            }
          }
          
          $offers[] = $offer;
        }
      }
      
      return $offers;
    }
    
    public static function parseReduceriMaxime($url, $debug = false)
    {
      $offers = array();
      
      $extra_offers = SiteParser::parseReduceriMaximeForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseReduceriMaximeOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseReduceriMaximeForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'promotions' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'main_container' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'h1' && $childNode2->hasChildNodes())
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {
                    if($childNode3->nodeName == 'a')
                    {
                      $extra_offers[] = $childNode3->getAttribute('href');
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseReduceriMaximeOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));
      
      $offer_description = trim(Utils::subString(trim(strip_tags(str_replace('</li>',"\n",Utils::subString($html,'<div class="desc_short">','</div>')))), 'Caracteristici ofertă'));
      $offer_description .= "\n";
      $offer_description .= trim(Utils::subString(trim(strip_tags(str_replace('</li>',"\n",Utils::subString($html,'<div class="terms">','</div>')))), 'Condiţii de utilizare'));
      
      $offer_id = Utils::subString($html,'http://www.reducerimaxime.ro/comanda/','"');
      
      $offer_discount = trim(strip_tags(Utils::subString($html,'<ul class="values">','</ul>')));
      $offer_discount = intval(Utils::subString($offer_discount, 'Lei', '%'));
      if($offer_discount >= 100)
      {
        $offer_discount = '';
      }
      
      $left_time = Utils::subString($html,'var timer_event = new Date("','")');
      $left_time = strtotime($left_time) - time();
        
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'images' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'img')
            {
              $offer_images[] = $childNode->getAttribute('src');
            }
          }
        }
      }
       
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && !is_null($offer_discount))
      {
        $unique_id = 'reducerimaxime.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseTheRealDeal($url, $debug = false)
    {
      $offers = array();
      
      $offers = array();
      $offers[] = SiteParser::parseTheRealDealOffer($url);
      
      $extra_offers = SiteParser::parseTheRealDealForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseTheRealDealOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseTheRealDealForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'ribbon' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseTheRealDealOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>Oferta zilei:','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h3>Informatii oferta</h3>','<h3>')));
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'cumpara' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $offer_id = Utils::subString($childNode->getAttribute('href'), 'http://www.therealdeal.ro/index.php?s=all&ss=pay&id=');
              break;
            }
          }
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'bn-c')
        {
          $offer_discount = intval($div->nodeValue);
          break;
        }
      }
      
      if(!$offer_discount || $offer_discount >= 100)
      {
        $offer_discount = 0;
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if(intval($p) > $offer_discount)
              {
                $offer_discount = intval($p);
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      $left_time = Utils::subString($html,"limit: '","'") - time();
        
      if($div = $res->getElementById('gallery'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'img')
                {
                  $offer_images[] = 'http://www.therealdeal.ro'.$childNode2->getAttribute('src');
                }
              }
            }
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'therealdeal.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseProDeal($url, $debug = false)
    {
      $offers = array();
      
      $offers = array();
      $offers[] = SiteParser::parseProDealOffer($url);
      
      $extra_offers = SiteParser::parseProDealForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseProDealOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseProDealForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if(strpos($div->getAttribute('class'), 'deal_block') === 0 && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseProDealOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 class="deal-title">','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h4>Beneficii:</h4>','</div>')));
      
      $spans = $res->getElementsByTagName('span');
      foreach($spans as $span)
      {
        if($span->getAttribute('class') == 'deal-purchase deal-button' && $span->hasChildNodes())
        {
          foreach($span->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $offer_id = Utils::subString($childNode->getAttribute('href'), null, '/?group-buying-provider-payment-page');
              break;
            }
          }
        }
      }
      
      $spans = $res->getElementsByTagName('span');
      foreach($spans as $span)
      {
        if($span->getAttribute('class') == 'deal-savings')
        {
          $offer_discount = intval($span->nodeValue);
          break;
        }
      }
      
      $left_time = strtotime(Utils::subString($html,"var date = new Date ( '","' );")) - time();
      
      $imgs = $res->getElementsByTagName('img');
      foreach($imgs as $img)
      {
        if($img->getAttribute('class') == 'attachment-deal_thumbnail wp-post-image')
        {
          $offer_images[] = $img->getAttribute('src');
          break;
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'prodeal.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseWaz($url, $debug = false)
    {
      $offers = array();
      
      $extra_offers = SiteParser::parseWazForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseWazOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseWazForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $h2s = $res->getElementsByTagName('h2');
      foreach($h2s as $h2)
      {
        if($h2->hasChildNodes())
        {
          foreach($h2->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = 'http://www.waz.ro'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseWazOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_price = null;
      $offer_discount_price = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<div class="caracteristici">','</div>')));
      $offer_description = trim(Utils::subString($offer_description,'Descriere'));
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'comanda')
        {
          $offer_id = Utils::subString($link->getAttribute('href'), 'id=', '"');
          break;
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'offer-box')
        {
          if(!$offer_price)
          {
            $offer_price = floatval(preg_replace('/[^0-9.,]/','',$div->nodeValue));
            continue;
          }
          if(!$offer_discount_price)
          {
            $offer_discount_price = floatval(preg_replace('/[^0-9.,]/','',$div->nodeValue));
            break;
          }          
        }
      }
      
      if($offer_price && $offer_discount_price)
      {
        // $offer_discount = round($offer_discount_price * 100 / $offer_price);
      }
      
      $left_time = strtotime(Utils::subString($html,'<span class="target-date">', '</span>')) - time();
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'image' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'center' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'img')
                {
                  $offer_images[] = $childNode2->getAttribute('src');
                  break;
                }
              }
            }
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'waz.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parsePontomat($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parsePontomatOffer($url, $debug);
      
      $extra_offers = SiteParser::parsePontomatForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parsePontomatOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parsePontomatForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'box nextDealItem' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = 'http://www.pontomat.ro'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parsePontomatOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(Utils::subString(trim(strip_tags(Utils::subString($html,'<h1 class="fancy">','</h1>'))), 'Pontul zilei'));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h3>Caracteristici</h3>','<h3>Validitatea Cuponului</h3>')));
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'buy')
        {
          $offer_id = Utils::subString($link->getAttribute('href'), '/buy/', '"');
          break;
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'value')
        {
          $offer_discount = intval($div->nodeValue);
          break;
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'endTimestamp hidden')
        {
          $left_time = $div->nodeValue - time();
          break;
        }
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'mainImg' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'wrap' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'img')
                {
                  $offer_images[] = 'http://www.pontomat.ro'.$childNode2->getAttribute('src');
                }
              }
            }
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'pontomat.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parsePromoRomania($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parsePromoRomaniaOffer($url, $debug);
      
      $extra_offers = SiteParser::parsePromoRomaniaForExtraOffers($url);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parsePromoRomaniaOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parsePromoRomaniaForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'k_offer_left')
        {
          if($onclick = $div->getAttribute('onclick'))
          {
            $extra_offers[] = Utils::subString($onclick, 'document.location.href="', '"');
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parsePromoRomaniaOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 style="padding-top:13px;">','</h1>')));

      /*
      $tds = $res->getElementsByTagName('td');
      $i = 0;
      foreach($tds as $td)
      {
        $i++;
        if($i == 6 && $td->getAttribute('class') == 'offer_left')
        {
          $offer_description = trim(strip_tags($td->nodeValue));
          break;
        }
      }
      */
     
      $offer_description = trim(strip_tags(Utils::subString($html,'<div class="offer_content">','</table>')));
      
      $offer_id = Utils::subString($html, "top.location = 'http://www.promo-romania.ro/index.php?page=pay/card&id=", "'");
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'offer_procent')
        {
          $offer_discount = intval(trim(Utils::subString($div->nodeValue, "Discount", "%")));
          break;
        }
      }
      
      $left_time = Utils::subString($html, 'timer("hours", "mins", "sec",', ');');
      $parts = explode(',', $left_time);
      if(isset($parts[0]))
      {
        $year = trim($parts[0]);
      }
      if(isset($parts[1]))
      {
        $month = trim($parts[1]);
      }
      if(isset($parts[2]))
      {
        $day = trim($parts[2]);
      }
      if(isset($parts[3]))
      {
        $hour = trim($parts[3]);
      }
      if(isset($parts[4]))
      {
        $min = trim($parts[4]);
      }
      if(isset($parts[5]))
      {
        $sec = trim($parts[5]);
      }
      if(isset($year) && isset($month) && isset($day) && isset($hour) && isset($min) && isset($sec))
      {
        $left_time = strtotime($year.'-'.$month.'-'.$day.' '.$hour.':'.$min.':'.$sec) - time();
      }
      
      if($img = $res->getElementById('offer_image'))
      {
        $offer_images[] = $img->getAttribute('src');
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'promo-romania.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseSaveMoney($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseSaveMoneyOffer($url, $debug);
      
      $extra_offers = SiteParser::parseSaveMoneyForExtraOffers($url);
      if($extra_offers[0])
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseSaveMoneyForExtraOffers($extra_offers[0]));
      }
      
      $extra_offers = array_unique($extra_offers);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseSaveMoneyOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseSaveMoneyForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'extra-img' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = 'http://www.savemoney.ro'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseSaveMoneyOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));

      $offer_description = trim(strip_tags(Utils::subString($html,'<h3>Beneficii</h3>','</div>')));
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'cumpara ')
        {
          $offer_id = Utils::subString($link->getAttribute('href'), 'add=', '"');
          break;
        }
      }

      $offer_discount = trim(Utils::subString(trim(strip_tags(Utils::subString($html,'<div class="reducere_app">','</div>'))), 'RON', '%'));
      
      $left_time = trim(Utils::subString($html,'<body onload="dd_countdown(',')'));
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'slideshow' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'img')
                {
                  $offer_images[] = 'http://www.savemoney.ro'.$childNode2->getAttribute('src');
                }
              }
            }
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'savemoney.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseLocalDeals($url, $debug = false)
    {
      $offers = array();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $xml = new SimpleXMLElement($html);
      
      foreach($xml->channel->item as $deal)
      {
        $offers[] = SiteParser::parseLocalDealsOffer($deal);
      }
        
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseLocalDealsOffer($xml, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
       
      $offer_title = (string)$xml->title;
      $offer_url = (string)$xml->link;
      $offer_description = trim(strip_tags((string)$xml->description));
      $offer_id = Utils::subString((string)$xml->link, 'http://www.localdeals.ro/deals/');
      $offer_discount = (string)$xml->discount;
      $left_time = strtotime((string)$xml->end_date) - time();
      $offer_images[] = (string)$xml->enclosure['url'];
      
      $cities = array();
      if($city = (string)$xml->city)
      {
        $cities = CityPeer::guessCitiesByName($city);
      }
      /*
      var_dump($offer_id);
      var_dump((string)$xml->city);
      foreach($cities as $city)
      {
        var_dump($city->getId());
        var_dump($city->getName());
      }
      var_dump('---');
      */
      if(!$offer_discount)
      {
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if($p > $offer_discount)
              {
                $offer_discount = $p;
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        var_dump($offer_url);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && !is_null($offer_discount))
      {
        $unique_id = 'localdeals.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseGogetit($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseGogetitOffer($url, $debug);
      
      $extra_offers = SiteParser::parseGogetitForExtraOffers($url);
      if($extra_offers[0])
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseGogetitForExtraOffers($extra_offers[0]));
      }
      
      $extra_offers = array_unique($extra_offers);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseGogetitOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseGogetitForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'side-block' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'block-content' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'div' && $childNode2->getAttribute('class') == 'midpix' && $childNode2->hasChildNodes())
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {  
                    if($childNode3->nodeName == 'div' && $childNode3->getAttribute('class') == 'midtxt' && $childNode3->hasChildNodes())
                    {
                      foreach($childNode3->childNodes as $childNode4)
                      {  
                        if($childNode4->nodeName == 'a')
                        {
                          $extra_offers[] = $childNode4->getAttribute('href');
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseGogetitOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));
      if(strpos($offer_title, "Oferta Zilei:") !== false)
      {
        $offer_title = trim(Utils::subString($offer_title, 'Oferta Zilei:'));
      }

      $offer_description = trim(strip_tags(Utils::subString($html,'<h3>Beneficii</h3>','</div>')));
      
      if($link = $res->getElementById('scriptbuynow'))
      {
        $offer_id = Utils::subString($link->getAttribute('href'),'/product/','/');
      }
      
      $offer_discount = trim(strip_tags(Utils::subString($html,'<table class="discount_sageata"><tr><td>-','%')));

      if($offer_discount >= 100)
      {
        $offer_discount = '';
      }
      
      $left_time = trim(Utils::subString($html,'var targetdate = "','";'));
      $left_time = strtotime($left_time);
      $left_time = $left_time - time();
      
      if($div = $res->getElementById('image'))
      {
        if($div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'img')
            {
              $offer_images[] = $childNode->getAttribute('src');
            }
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && !is_null($offer_discount))
      {
        $unique_id = 'gogetit.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseWisebuy($url, $debug = false)
    {
      $offers = array();
      $offers[] = SiteParser::parseWisebuyOffer($url, $debug);
      
      $extra_offers = SiteParser::parseWisebuyForExtraOffers($url);
      if($extra_offers[0])
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseWisebuyForExtraOffers($extra_offers[0]));
      }
      
      $extra_offers = array_unique($extra_offers);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseWisebuyOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseWisebuyForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'featured_deal_image' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = 'http://www.wisebuy.ro'.$childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseWisebuyOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h2>','</h2>')));

      $offer_description = trim(strip_tags(Utils::subString($html,'<h3>Descriere oferta</h3>','</div>')));

      $offer_id = Utils::subString($html,'/purchase.html?cid=','"');
      
      $offer_discount = trim(Utils::subString(trim(strip_tags(Utils::subString($html,'<tr class="panel_buy_discount">','</tr>'))), 'REDUCERE', '%'));

      $left_time = trim(Utils::subString($html,"\tdateFuture = new Date(",');'));
      $parts = explode(',', $left_time);
      $year = Utils::subString($parts[0],"'", "'");
      $month = Utils::subString($parts[1],"'", "'") + 1;
      $day = Utils::subString($parts[2],"'", "'");
      $hour = Utils::subString($parts[3],"'", "'");
      $minute = Utils::subString($parts[4],"'", "'");
      $seconds = Utils::subString($parts[5],"'", "'");
      $left_time = strtotime($year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$seconds) - time();
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'deal_image' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'wt-rotator')
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'div' && $childNode2->getAttribute('class') == 'c-panel')
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {
                    if($childNode3->nodeName == 'div' && $childNode3->getAttribute('class') == 'thumbnails')
                    {
                      foreach($childNode3->childNodes as $childNode4)
                      {
                        if($childNode4->nodeName == 'ul')
                        {
                          foreach($childNode4->childNodes as $childNode5)
                          {
                            if($childNode5->nodeName == 'li')
                            {
                              foreach($childNode5->childNodes as $childNode6)
                              {
                                if($childNode6->nodeName == 'a')
                                {
                                  $offer_images[] = 'http://www.wisebuy.ro'.$childNode6->getAttribute('href');
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          break;
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'wisebuy.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseOferteiasi($url, $debug = false)
    {
      $offers = array();
      
      $url = 'http://www.oferteiasi.ro/distreaza-te.php';
      $extra_offers = SiteParser::parseOferteiasiForExtraOffers($url);
      $url = 'http://www.oferteiasi.ro/savureaza.php';
      $extra_offers = array_merge($extra_offers, SiteParser::parseOferteiasiForExtraOffers($url));
      $url = 'http://www.oferteiasi.ro/ai-grija-de-tine.php';
      $extra_offers = array_merge($extra_offers, SiteParser::parseOferteiasiForExtraOffers($url));
      $url = 'http://www.oferteiasi.ro/descopera.php';
      $extra_offers = array_merge($extra_offers, SiteParser::parseOferteiasiForExtraOffers($url));
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseOferteiasiOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseOferteiasiForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'modmare' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'div' && $childNode->getAttribute('class') == 'right')
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'a')
                {
                  $extra_offers[] = 'http://www.oferteiasi.ro/'.$childNode2->getAttribute('href');
                }
              }
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseOferteiasiOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'</h1>','</div>')));

      $offer_id = Utils::subString($url,'http://www.oferteiasi.ro/vezioferta.php?oferta=');
      
      $offer_discount = trim(strip_tags(Utils::subString($html,'<div class="reducere">','%')));

      $left_time = trim(Utils::subString($html,'TargetDate = "','";'));
      $left_time = strtotime($left_time) - time();
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'bigpict' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'img')
            {
              $offer_images[] = 'http://www.oferteiasi.ro/'.$childNode->getAttribute('src');
            }
          }
          break;
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'oferteiasi.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseOfertapret($url, $debug = false)
    {
      $offers = array();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $xml = new SimpleXMLElement($html);
      
      foreach($xml->channel->item as $deal)
      {
        $offers[] = SiteParser::parseOfertapretOffer($deal);
      }
        
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseOfertapretOffer($xml, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      $offer_title = (string)$xml->title;
      $offer_url = (string)$xml->link;
      $offer_description = trim(strip_tags((string)$xml->description));
      $offer_id = (string)$xml->id;
      $offer_discount = (string)$xml->discount;
      $left_time = strtotime((string)$xml->end_date) - time();
      $attr = $xml->enclosure->attributes();
      $offer_images[] = (string)$attr['url'];
      
      $cities = array();
      if($city = (string)$xml->city)
      {
        $cities = CityPeer::guessCitiesByName($city);
      }
      else
      {
        $cities = CityPeer::guessCitiesByName('national');
      }
      /*
      var_dump($offer_id);
      var_dump((string)$xml->city);
      foreach($cities as $city)
      {
        var_dump($city->getId());
        var_dump($city->getName());
      }
      var_dump('---');
      */
      if(!$offer_discount)
      {
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if($p > $offer_discount)
              {
                $offer_discount = $p;
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if(count($cities) && $offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_url)
      {
        $unique_id = 'ofertapret.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }

        foreach($offer->getOfferCitys() as $offer_city)
        {
          try
          {
            $offer_city->delete();
          }
          catch(Exception $e)
          {
            // var_dump($e->getMessage());
          }
        }
        
        foreach($cities as $city)
        {
          $offer_city = new OfferCity();
          $offer_city->setCityId($city->getId());
          $offer_city->setOfferId($offer->getId());
          $offer_city->save();
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseShopdeals($url, $debug = false)
    {
      $offers = array();

      $offers[] = SiteParser::parseShopdealsOffer($url);
      
      $extra_offers = SiteParser::parseShopdealsForExtraOffers($url);
      if($extra_offers[0])
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseShopdealsForExtraOffers($extra_offers[0]));
      }
      
      $extra_offers = array_unique($extra_offers);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseShopdealsOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseShopdealsForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $links = $res->getElementsByTagName('a');
      foreach($links as $link)
      {
        if($link->getAttribute('class') == 'vezi-oferta')
        {
          $extra_offers[] = 'http://www.shopdeals.ro'.$link->getAttribute('href');
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseShopdealsOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1>','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h2>Informatii Oferta</h2>','</div>')));

      $offer_id = Utils::subString($html,'/?page=cart&amp;add=','"');
      
      $trs =  $res->getElementsByTagName('tr');
      foreach($trs as $tr)
      {
        if($tr->getAttribute('class') == 't-data1' && $tr->hasChildNodes())
        {
          $i = 0;
          foreach($tr->childNodes as $childNode)
          {
            $i++;
            if($i == 1 && $childNode->nodeName == "td")
            {
              $full_price = str_replace('.','',$childNode->nodeValue);
              $full_price = floatval(str_replace(',','.',$full_price));
            }
            if($i == 2 && $childNode->nodeName == "td")
            {
              $discount = str_replace('.','',$childNode->nodeValue);
              $discount = floatval(str_replace(',','.',$discount));
              $offer_discount = round($discount * 100 / $full_price);
            }
          }
        }
      }

      $left_time = trim(Utils::subString($html,'dd_countdown(',');'));
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'slideshow' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a' && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == 'img')
                {
                  $offer_images[] = 'http://www.shopdeals.ro'.$childNode2->getAttribute('src');
                }
              }
            }
          }
          break;
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time && $offer_discount)
      {
        $unique_id = 'shopdeals.ro:'.$offer_id;
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseOfertadelux($url, $debug = false)
    {
      $offers = array();

      //offers[] = SiteParser::parseOfertadeluxOffer($url);
      
      $extra_offers = SiteParser::parseOfertadeluxForExtraOffers($url);
      if($extra_offers[0])
      {
        $extra_offers = array_merge($extra_offers, SiteParser::parseOfertadeluxForExtraOffers($extra_offers[0]));
      }
      
      $extra_offers = array_unique($extra_offers);
      
      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseOfertadeluxOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseOfertadeluxForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $spans = $res->getElementsByTagName('span');
      foreach($spans as $span)
      {
        if($span->getAttribute('class') == 'hold-buy-now' && $span->hasChildNodes())
        {
          foreach($span->childNodes as $link)
          {
            $extra_offers[] = 'http://www.ofertadelux.ro/'.$link->getAttribute('href');
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseOfertadeluxOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
      
      // $html = file_get_contents($url);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 class="pad20">','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<h2 class="blue">Informatii despre oferta</h2>','<script')));

      $offer_id = Utils::subString($html,'<span class="hold-buy-now"><a href="cumpara/','/"');
      
      $offer_discount = Utils::subString($html,'<li>Reducere<span><strong>','%</strong>');
      $offer_discount = intval(preg_replace('/[^0-9.,]/','',$offer_discount));
      
      if($offer_discount == 100)
      {
        $offer_discount = null;
      }
 
      $left_time = trim(Utils::subString($html,'var ofertas = new Date(',');'));
      $parts = explode(',', $left_time);
      $left_time = strtotime($parts[0].'-'.intval($parts[1]).'-'.$parts[2].' '.$parts[3].':'.$parts[4].':'.$parts[5]);
      $left_time = $left_time - time();
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'ps_image_wrapper' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'img')
            {
              $offer_images[] = 'http://www.ofertadelux.ro/'.$childNode->getAttribute('src');
            }
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'ofertadelux.ro:'.md5($offer_id);
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseKupedia($url, $debug = false)
    {
      $offers = array();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $xml = new SimpleXMLElement($html);
      
      foreach($xml->deals->deal as $deal)
      {
        $offers[] = SiteParser::parseKupediaOffer($deal);
      }
        
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseKupediaOffer($xml, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
       
      $offer_title = (string)$xml->title;
      $offer_url = (string)$xml->url;
      $offer_description = trim((string)$xml->description);
      $offer_id = (string)$xml->id;
      $offer_discount = (string)$xml->discount;
      $left_time = strtotime((string)$xml->deal_end) - time();
      $offer_images[] = (string)$xml->image;

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'kupedia.ro:'.md5($offer_id);
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseBuyzilla($url, $debug = false)
    {
      $offers = array();

      $extra_offers = SiteParser::parseBuyzillaForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseBuyzillaOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseBuyzillaForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'or_titlu' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseBuyzillaOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $offer_title = trim(strip_tags(Utils::subString($html,'<h1 class="titlu-big fl">','</h1>')));
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<div id="oferta">','</div')));

      $offer_id = Utils::subString($html,'<input type="hidden" name="oferta_id" value="','"');
            
      if(!$offer_discount)
      {
        $offer_discount = 0;
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if(intval($p) > $offer_discount)
              {
                $offer_discount = intval($p);
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      
      if(!$offer_discount)
      {
        $offer_discount = trim(strip_tags(Utils::subString($html,'<span class="disco"> Discount<br />','%')));
      }
 
      $hours = $minutes = $seconds = null;
      if($span = $res->getElementById('hours'))
      {
        $hours = $span->nodeValue;
      }
      if($span = $res->getElementById('minutes'))
      {
        $minutes = $span->nodeValue;
      }
      if($span = $res->getElementById('seconds'))
      {
        $seconds = $span->nodeValue;
      }
      if(!is_null($hours) && !is_null($minutes) && !is_null($seconds))
      {
        $left_time = $hours * 3600 + $minutes * 60 + $seconds;
      }
      
      if($div = $res->getElementById('slideshow'))
      {
        foreach($div->childNodes as $childNode)
        {
          if($childNode->nodeName == 'img')
          {
            $offer_images[] = $childNode->getAttribute('src');
            break;
          }
        }
      }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'buyzilla.ro:'.md5($offer_id);
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseDealmania($url, $debug = false)
    {
      $offers = array();

      $offers[] = SiteParser::parseDealmaniaOffer($url);
      
      $extra_offers = SiteParser::parseDealmaniaForExtraOffers($url);

      foreach($extra_offers as $url)
      {
        $offers[] = SiteParser::parseDealmaniaOffer($url);
      }
       
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseDealmaniaForExtraOffers($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $extra_offers = array();
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'deal_extra_list_item clearfix' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'a')
            {
              $extra_offers[] = $childNode->getAttribute('href');
            }
          }
        }
      }
      
      return $extra_offers;
    }
    
    public static function parseDealmaniaOffer($url, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $res = new DomDocument('1.0', 'utf8');
      $res->validateOnParse = true;
      @$res->loadHTML($html);
       
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'offer_description' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == 'h2')
            {
              $offer_title = trim(strip_tags($childNode->nodeValue));
            }
          }
        }
      }
      
      $offer_description = trim(strip_tags(Utils::subString($html,'<div id="offer_description_short">','</div')));
      if($pos = strpos($offer_description, 'citeste mai mult'))
      {
        $offer_description = substr($offer_description, 0, $pos);
      }

      $offer_id = Utils::subString($html,'<a href="/shop/cart/add.php?product_id=','&quantity=1');
      
      $offer_discount = Utils::subString($html,'<li class="btn innerShadow"><span>Reducere','%');
      if(!$offer_discount)
      {
        $offer_discount = 0;
        preg_match_all('/[0-9]+%/', $offer_title, $matches);
        if($matches[0])
        {
          if(is_array($matches[0]))
          {
            foreach($matches[0] as $p)
            {
              if(intval($p) > $offer_discount && intval($p) < 100)
              {
                $offer_discount = intval($p);
              }
            }
          }
          else
          {
            $offer_discount = intval($matches[0]);
          }
        }
      }
      if($offer_discount >=100)
      {
        $offer_discount = null;
      }   
         
      $left_time = Utils::subString($html,'timeEnd = new Date(',')');
      $parts = explode(',', $left_time);
      if(count($parts) == 6)
      {
        $left_time = strtotime(trim($parts[0]).'-'.(trim($parts[1])+1).'-'.trim($parts[2]).' '.trim($parts[3]).':'.trim($parts[4]).':'.trim($parts[5])) - time();
      }
      else
      {
        $left_time = null;
      }
      
      $divs = $res->getElementsByTagName('div');
      foreach($divs as $div)
      {
        if($div->getAttribute('class') == 'fade_gallery' && $div->hasChildNodes())
        {
          foreach($div->childNodes as $childNode)
          {
            if($childNode->nodeName == "ul" && $childNode->hasChildNodes())
            {
              foreach($childNode->childNodes as $childNode2)
              {
                if($childNode2->nodeName == "li" && $childNode2->hasChildNodes())
                {
                  foreach($childNode2->childNodes as $childNode3)
                  {
                    if($childNode3->nodeName == "img")
                    {
                      $offer_images[] = 'http://www.dealmania.ro'.$childNode3->getAttribute('src');
                      break;
                    }
                  }
                }
              }
            }
          }
        }
      }
      $offer_images = array_shift($offer_images);
      
      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'dealmania.ro:'.md5($offer_id);
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl('http://www.dealmania.ro/deal-'.$offer_id.'-1.html');
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    
    public static function parseReduceriOferte($url, $debug = false)
    {
      $offers = array();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
      $xml = new SimpleXMLElement($html);
      
      foreach($xml->item as $deal)
      {
        $offers[] = SiteParser::parseReduceriOferteOffer($deal);
      }
        
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseReduceriOferteOffer($xml, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
       
      $offer_title = (string)$xml->title;
      $offer_url = (string)$xml->link_deal;
      $offer_description = trim(strip_tags((string)$xml->description));
      $offer_id = (string)$xml->id;
      $offer_discount = (string)$xml->discount;
      $left_time = strtotime((string)$xml->deal_end) - time();
      $offer_images[] = (string)$xml->image;

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if($offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'reduceri-oferte.ro:'.md5($offer_id);
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check_1 = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                $image_check_2 = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/medium/'.$offer_image->getImage(), 206, 206);                
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check_1 && $image_check_2)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        return $offer;
      }
      
      return null;
    }
    public static function parseEasyDeals($url, $debug = false)
    {
      $offers = array();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($ch);
      curl_close($ch);
      
	    $html = str_replace('&', '&amp;', $html);
	  
      $xml = new SimpleXMLElement($html);

      foreach($xml->deal as $deal)
      {
        $offers[] = SiteParser::parseEasyDealsOffer($deal);
      }
        
      $results = array();
      foreach($offers as $offer)
      {
        if($offer)
        {
          $results[$offer->getId()] = $offer;
        }
      }
      
      return $results;
    }
    
    public static function parseEasyDealsOffer($xml, $debug = false)
    {
      $offer_title = null;
      $offer_description = null;
      $offer_images = array();
      $left_time = null;
      $offer_discount = null;
      $offer_id = null;
      $offer_url = null;
       
      $offer_title = (string)$xml->title;
      $offer_url = (string)$xml->deal_url;
      $offer_description = trim((string)$xml->short_description);
      $offer_id = (string)$xml->deal_url;
      $offer_discount = (string)$xml->discount_percent;
      $left_time = strtotime((string)$xml->end_date) - time();
      $offer_images[] = (string)$xml->large_image_url;
	  
  	  $cities = array();
  	  if($city = (string)$xml->city)
  	  {
  	  	$cities = CityPeer::guessCitiesByName($city);
  	  }

      if($debug)
      {
        var_dump($offer_id);
        var_dump($offer_title);
        var_dump($offer_description);
        var_dump($offer_discount);
        var_dump($offer_images);
        var_dump($left_time);
        die();
      }
      
      if(count($cities) && $offer_id && $offer_url && $offer_title && $offer_description && $offer_images && $left_time)
      {
        $unique_id = 'easydeals.ro:'.md5($offer_id);
        $is_new = false;
        if(!$offer = OfferPeer::retrieveByUniqueId($unique_id))
        {
          $offer = new Offer();
          $is_new = true;
        }
        $offer->setUniqueId($unique_id);
        $offer->setName(html_entity_decode($offer_title));
        $offer->setDescription($offer_description);
        $offer->setDiscountPercent($offer_discount);
        $offer->setUrl($offer_url);
        $offer->setStartTime(date('Y-m-d 00:00:00', time()));
        $offer->setEndTime(date('Y-m-d H:i:s', time() + $left_time));
        
        $offer->save();
        
        if($is_new)
        {
          foreach($offer_images as $image)
          {
            if($image)
            {
              $offer_image = new OfferImage();
              $offer_image->setOfferId($offer->getId());
              $offer_image->setUrl($image);
              $offer_image->save();
              $offer_image->setImage($offer_image->getId().'.jpg');
              if(Utils::copyImageFromUrl($offer_image->getUrl(), $offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/'))
              {
                $image_check = Utils::resizePhoto(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage(), sfConfig::get('sf_upload_dir').'/offers/small/'.$offer_image->getImage(), 90, 90);
                unlink(sfConfig::get('sf_upload_dir').'/offers/'.$offer_image->getImage());
                if($image_check)
                {
                  $offer_image->save();
                  break;
                }
                else
                {
                  $offer_image->delete();
                }
              }
              else
              {
                $offer_image->delete();
              }
            }
          }
        }
        
        foreach($offer->getOfferCitys() as $offer_city)
        {
          try
          {
            $offer_city->delete();
          }
          catch(Exception $e)
          {
            // var_dump($e->getMessage());
          }
        }
        
        foreach($cities as $city)
        {
          $offer_city = new OfferCity();
          $offer_city->setCityId($city->getId());
          $offer_city->setOfferId($offer->getId());
          $offer_city->save();
        }
        return $offer;
      }
      
      return null;
    }
  }

